<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">

		
	<!-- 手术操作章节 -->
	<xsl:template match="*" mode="HistoryOfProceduresSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HistoryOfProcedures]" mode="HistoryOfProceduresEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 手术操作章节条目 -->
	<xsl:template match="*" mode="HistoryOfProceduresEntry">
		<xsl:apply-templates select="hl7:entry/hl7:procedure" mode="HCCProcedure">
			<xsl:with-param name="sectionType" select="'HP'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!-- 护理操作章节 -->
	<xsl:template match="*" mode="NursingOperationsSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_NursingOperations]" mode="NursingOperationsEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 护理操作章节条目 -->
	<xsl:template match="*" mode="NursingOperationsEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCProcedure">
			<xsl:with-param name="sectionType" select="'NP'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 其他处置章节 -->
	<xsl:template match="*" mode="OtherOperationsSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_OtherOperations]" mode="OtherOperationsEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 其他处置章节条目 -->
	<xsl:template match="*" mode="OtherOperationsEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCProcedure">
			<xsl:with-param name="sectionType" select="'OP'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 输血章节 (只处理 11 麻醉记录、13 输血记录)-->
	<xsl:template match="*" mode="HistoryOfBloodTransfusionSection">
		<xsl:if test="(($hccDocType='C0011') or ($hccDocType='C0013'))">
			<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HistoryOfBloodTransfusion]" mode="HistoryOfBloodTransfusionEntry">
			</xsl:apply-templates>
		</xsl:if>
	</xsl:template>
	<!-- 输血章节条目 -->
	<xsl:template match="*" mode="HistoryOfBloodTransfusionEntry">
		<xsl:apply-templates select="hl7:entry/hl7:procedure" mode="HCCProcedure">
			<xsl:with-param name="sectionType" select="'HBT'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 抢救记录章节 -->
	<xsl:template match="*" mode="RescueRecordSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_RescueRecord]" mode="RescueRecordEntry">
		</xsl:apply-templates>
	</xsl:template>	
	<!-- 抢救记录章节条目 -->
	<xsl:template match="*" mode="RescueRecordEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation[hl7:code/@code='DE06.00.181.00']" mode="HCCProcedure">
			<xsl:with-param name="sectionType" select="'RR'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 引流章节 -->
	<xsl:template match="*" mode="SurgicalDrainsSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_SurgicalDrains]" mode="SurgicalDrainsEntry">
		</xsl:apply-templates>
	</xsl:template>	
	<!-- 引流章节条目 （只处理引流标志不是false的记录）-->
	<xsl:template match="*" mode="SurgicalDrainsEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation[(hl7:code/@code='DE05.10.165.00') and (hl7:value/@value !='false')]" mode="HCCProcedure">
			<xsl:with-param name="sectionType" select="'SD'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!-- 卫生事件章节 -->
	<xsl:template match="*" mode="HealthcareEventOperationsSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_HealthcareEvent]" mode="HealthcareEventOperationsEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 卫生事件章节条目 -->
	<xsl:template match="*" mode="HealthcareEventOperationsEntry">
		<xsl:apply-templates select="hl7:entry/hl7:procedure" mode="HCCProcedure">
			<xsl:with-param name="sectionType" select="'HEO'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!--通用处置条目输出模板-->
	<xsl:template match="*" mode="HCCProcedure">
		<xsl:param name = "sectionType" />
			<Procedure>
			<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
			<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
			<xsl:choose>
			<xsl:when test="$sectionType='HP'">
				<Procedure>
					<Code><xsl:value-of select="hl7:code/@code"/></Code>
					<Description><xsl:value-of select="hl7:code/@displayName"/></Description>
				</Procedure>
				<xsl:choose>
					<xsl:when test="string-length(hl7:effectiveTime/hl7:high/@value)">
						<ProcedureTime><xsl:value-of select="isc:evaluate('xmltimestamp',hl7:effectiveTime/hl7:high/@value)"/></ProcedureTime>
					</xsl:when>
					<xsl:when test="string-length(hl7:effectiveTime/hl7:low/@value)">
						<ProcedureTime><xsl:value-of select="isc:evaluate('xmltimestamp',hl7:effectiveTime/hl7:low/@value)"/></ProcedureTime>
					</xsl:when>
				</xsl:choose>
				<!--避免数据为"-"的数据被处理-->
				<xsl:if test="string-length(hl7:performer/hl7:assignedEntity/hl7:id/@extension)>1">
				<Clinician>
					<Code><xsl:value-of select="hl7:performer/hl7:assignedEntity/hl7:id/@extension"/></Code>
					<Name><GivenName><xsl:value-of select="hl7:performer/hl7:assignedEntity/hl7:assignedPerson/hl7:name"/></GivenName></Name>
					<CareProviderType>
						<Code><xsl:value-of select="'手术者'"/></Code>
						<Description><xsl:value-of select="'手术者'"/></Description>
					</CareProviderType>
				</Clinician>
				</xsl:if>				
			</xsl:when>
			
			<xsl:when test="$sectionType='NP'">
				<Procedure>
					<Code><xsl:value-of select="hl7:value"/></Code>
					<Description><xsl:value-of select="hl7:value"/></Description>
				</Procedure>
			</xsl:when>
			
			<xsl:when test="$sectionType='HEO'">
				<Procedure>
					<Code><xsl:value-of select="hl7:code/@code"/></Code>
					<Description><xsl:value-of select="hl7:code/@displayName"/></Description>
				</Procedure>
			</xsl:when>
			
			<xsl:when test="$sectionType='OP'">
				<Procedure>
					<Code><xsl:value-of select="'其他处置'"/></Code>
					<Description><xsl:value-of select="'其他处置'"/></Description>
				</Procedure>
			</xsl:when>
			
			<xsl:when test="$sectionType='HBT'">
				<Procedure>
					<Code><xsl:value-of select="'输血'"/></Code>
					<Description><xsl:value-of select="'输血'"/></Description>
				</Procedure>
				<ProcedureTime><xsl:value-of select="isc:evaluate('xmltimestamp',hl7:effectiveTime/hl7:high/@value)"/></ProcedureTime>
			</xsl:when>
			
			<xsl:when test="$sectionType='RR'">
				<Procedure>
					<Code><xsl:value-of select="'急诊抢救'"/></Code>
					<Description><xsl:value-of select="'急诊抢救'"/></Description>
				</Procedure>
				<ProcedureTime><xsl:value-of select="hl7:effectiveTime/hl7:low"/></ProcedureTime>
				<!--
				<xsl:if test="string-length(hl7:entryRelationship/hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE08.30.032.00']/hl7:value)">
				<Clinician>
					<Code><xsl:value-of select="hl7:entryRelationship/hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE08.30.032.00']/hl7:value"/></Code>
					<Name><GivenName><xsl:value-of select="hl7:entryRelationship/hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE08.30.032.00']/hl7:value"/></GivenName></Name>
					<CareProviderType>
						<Code><xsl:value-of select="'参加抢救人员'"/></Code>
						<Description><xsl:value-of select="'参加抢救人员'"/></Description>
					</CareProviderType>
				</Clinician>
				</xsl:if>-->
			</xsl:when>
			
			<xsl:when test="$sectionType='SD'">
				<Procedure>
					<Code><xsl:value-of select="'引流'"/></Code>
					<Description><xsl:value-of select="'引流'"/></Description>
				</Procedure>
				<Devices>
					<Device>
						<Code><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.044.00']/hl7:value"/></Code>
						<Description><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.044.00']/hl7:value"/></Description>
					</Device>
				</Devices>
			</xsl:when> 
			</xsl:choose>
			</Procedure>		
	</xsl:template>
	
</xsl:stylesheet>
